package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BaseDao;
import com.delivery.www.dao.UserDao;
import com.delivery.www.po.Ban;
import com.delivery.www.po.User;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 12970
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User userNameLogin(String userName, String password) {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select * from user where user_name=? and password=?";
        Object[] params = {userName, password};
        User user = BaseDao.getForOne(connection, User.class, sql, params);
        JdbcUtils.closeResource(connection, null, null);
        return user;
    }

    @Override
    public User emailLogin(String email, String password) {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select * from user where email=? and password=?";
        Object[] params = {email, password};
        User user = BaseDao.getForOne(connection, User.class, sql, params);
        JdbcUtils.closeResource(connection, null, null);
        return user;
    }

    @Override
    public int register(String userName, String password, Float balance, String email, int identity) {
        Connection connection = JdbcUtils.getConnection();
        String sql = "insert into user (user_name,password,balance,email,identity) values (?,?,?,?,?)";
        Object[] params = {userName, password, balance, email, identity};
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public User recharge(int userId, Float value) {
        Connection connection = JdbcUtils.getConnection();
        Object[] params = {value, userId};
        String sql = "update user set balance = balance+? where user_id = ?";
        int i;
        User user = null;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, params);
            if (i > 0) {
                String sql1 = "select user_id,user_name,password,balance,email,identity from user where user_id= ?";
                user = BaseDao.getForOne(connection, User.class, sql1, userId);
            }
            JdbcUtils.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public List<User> getUser() {
        Connection connection = JdbcUtils.getConnection();
        List<User> users = null;
        String sql = "select user_id,user_name,password,balance,email,identity from user where identity!=3";
        if (connection != null) {
            users = BaseDao.getList(connection, User.class, sql);
        }
        JdbcUtils.closeResource(connection, null, null);
        return users;
    }

    @Override
    public List<User> getBanUser(List<Ban> bans) {
        Connection connection = JdbcUtils.getConnection();
        List<User> users = new ArrayList<>();
        String sql = "select user_id,user_name,password,balance,email,identity from user where user_id=?";
        for (Ban ban : bans) {
            if (connection != null) {
                Integer userId = ban.getUserId();
                User user = BaseDao.getForOne(connection, User.class, sql, userId);
                users.add(user);
            }
        }
        JdbcUtils.closeResource(connection, null, null);
        return users;
    }

    @Override
    public User getUserById(int userId) {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select user_id,user_name,password,balance,email,identity from user where user_id = ?";
        User user = null;
        if (connection != null) {
            user = BaseDao.getForOne(connection, User.class, sql, userId);
        }
        JdbcUtils.closeResource(connection, null, null);
        return user;
    }


}
